package com.smile.service.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.smile.model.dto.request.CameraAimDTO;
import com.smile.model.dto.request.DrcModeEnterRequestDTO;
import com.smile.model.dto.request.FlyToPointUpdateRequestDTO;
import com.smile.service.handler.AbstractDrcDownHandler;
import com.smile.service.handler.AbstractServicesHandler;
import com.smile.service.entity.drc.DroneEmergencyStopData;
import com.smile.service.entity.services.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Data
@Slf4j
public class FlyServicesExecutor {

    public static FlyServicesExecutor create() {
        return new FlyServicesExecutor();
    }


    private AbstractServicesHandler abstractServicesHandler = SpringUtil.getBean(AbstractServicesHandler.class);

    private String flightId;

    private String data;

    private String gateway;

    private String bid;
    private String tid;

//    private AbstractDrcDownHandler abstractDrcModeEnterHandler = SpringUtil.getBean(AbstractDrcDownHandler.class);


    public FlyServicesExecutor gateway(String gateway) {
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.gateway = gateway;
        return flyServicesExecutor;
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }

    public FlyServicesExecutor photoStorageSet(List<String> photoStorageSettings,
                                               String payloadIndex) {
        PhotoStorageSetData psd = new PhotoStorageSetData();
        psd.setPayloadIndex(payloadIndex);
        psd.setPhotoStorageSettings(photoStorageSettings);
        BaseServicesEntity<PhotoStorageSetData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(psd);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("photo_storage_set");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        return flyServicesExecutor;
    }
    public FlyServicesExecutor gimbalReset(Integer resetMode,
                                           String payloadIndex) {
        GimbalResetData gr = new GimbalResetData();
        gr.setPayloadIndex(payloadIndex);
        gr.setResetMode(resetMode);
        BaseServicesEntity<GimbalResetData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(gr);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("gimbal_reset");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("云台重置参数");
        System.out.println(flyServicesExecutor.getData());
        return flyServicesExecutor;
    }
    public FlyServicesExecutor CameraFocalLengthSet(String cameraType,Double zoomFactor,
                                           String payloadIndex) {
        CameraFocalLengthSetData cfls = new CameraFocalLengthSetData();
        cfls.setPayloadIndex(payloadIndex);
        cfls.setCameraType(cameraType);
        cfls.setZoomFactor(zoomFactor);
        BaseServicesEntity<CameraFocalLengthSetData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cfls);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_focal_length_set");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("变焦参数");
        System.out.println(JSON.toJSONString(flyServicesExecutor.getData()));
        return flyServicesExecutor;
    }


    public FlyServicesExecutor CameraScreenDrag(Boolean locked,Double pitchSpeed,Double yawSpeed,
                                                    String payloadIndex) {
        CameraScreenDragData cfls = new CameraScreenDragData();
        cfls.setPayloadIndex(payloadIndex);
        cfls.setLocked(locked);
        cfls.setPitchSpeed(pitchSpeed);
        cfls.setYawSpeed(yawSpeed);
        BaseServicesEntity<CameraScreenDragData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cfls);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_screen_drag");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("变焦参数");
        System.out.println(flyServicesExecutor.getData());
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraPointFocusAction(Float x,Float y,String cameraType,
                                                String payloadIndex) {
        CameraPointFocusActionData cpfa = new CameraPointFocusActionData();
        cpfa.setPayloadIndex(payloadIndex);
        cpfa.setX(x);
        cpfa.setY(y);

        cpfa.setCameraType(cameraType);
        BaseServicesEntity<CameraPointFocusActionData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cpfa);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_point_focus_action");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("点对焦框选变焦");
        System.out.println(flyServicesExecutor.getData());
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraAim(Float x,Float y,String cameraType,
                                                      String payloadIndex,Boolean locked) {
        CameraAimDTO caim = new CameraAimDTO();
        caim.setPayloadIndex(payloadIndex);
        caim.setX(x);
        caim.setY(y);

        caim.setCameraType(cameraType);
        caim.setLocked(locked);
        BaseServicesEntity<CameraAimDTO> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(caim);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_aim");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("aim");
        System.out.println(flyServicesExecutor.getData());
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraFrameZoom(Boolean locked,Float x,Float y,Float width,Float height,String cameraType,
                                               String payloadIndex) {
        CameraFrameZoomData cfzd = new CameraFrameZoomData();
        cfzd.setPayloadIndex(payloadIndex);
        cfzd.setLocked(locked);
        cfzd.setX(x);
        cfzd.setY(y);
        cfzd.setWidth(width);
        cfzd.setHeight(height);
        cfzd.setCameraType(cameraType);
        BaseServicesEntity<CameraFrameZoomData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cfzd);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_frame_zoom");
        String jsonString = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(jsonString);
        System.out.println("框选变焦");
        System.out.println(flyServicesExecutor.getData());
        return flyServicesExecutor;
    }

    public FlyServicesExecutor flyToPointStop() {
        FlyToPointStopEntity flyToPointStopEntity = new FlyToPointStopEntity();
        BaseServicesEntity<FlyToPointStopEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(flyToPointStopEntity);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("fly_to_point_stop");
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor payloadAuthorityGrab(String payloadIndex) {
        PayloadAuthorityGrabEntity payloadAuthorityGrabEntity = new PayloadAuthorityGrabEntity();
        payloadAuthorityGrabEntity.setPayloadIndex(payloadIndex);
        BaseServicesEntity<PayloadAuthorityGrabEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(payloadAuthorityGrabEntity);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("payload_authority_grab");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraPhotoTake(String payloadIndex) {
        CameraPhotoTake cameraPhotoTake = new CameraPhotoTake();
        cameraPhotoTake.setPayloadIndex(payloadIndex);
        BaseServicesEntity<CameraPhotoTake> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cameraPhotoTake);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_photo_take");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraPhotoStop(String payloadIndex) {
        CameraPhotoStop cameraPhotoStop = new CameraPhotoStop();
        cameraPhotoStop.setPayloadIndex(payloadIndex);
        BaseServicesEntity<CameraPhotoStop> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cameraPhotoStop);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_photo_stop");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraRecordStop(String payloadIndex) {
        CameraRecordingStopEntity cameraRecordingStopEntity = new CameraRecordingStopEntity();
        cameraRecordingStopEntity.setPayloadIndex(payloadIndex);
        BaseServicesEntity<CameraRecordingStopEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cameraRecordingStopEntity);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_recording_stop");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraModeSwitch(String payloadIndex, Integer cameraMode) {
        CameraModeSwitchEntity cameraModeSwitchEntity = new CameraModeSwitchEntity();
        cameraModeSwitchEntity.setPayloadIndex(payloadIndex);
        cameraModeSwitchEntity.setCameraMode(cameraMode);
        System.out.println(cameraMode);
        BaseServicesEntity<CameraModeSwitchEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cameraModeSwitchEntity);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_mode_switch");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor cameraRecordStart(String payloadIndex) {
        CameraRecordingStartEntity cameraRecordingStartEntity = new CameraRecordingStartEntity();
        cameraRecordingStartEntity.setPayloadIndex(payloadIndex);
        BaseServicesEntity<CameraRecordingStartEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(cameraRecordingStartEntity);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("camera_recording_start");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }


    public FlyServicesExecutor flighttaskRecovery() {
        FlighttaskRecoveryEntity flighttaskRecovery = new FlighttaskRecoveryEntity();
        BaseServicesEntity<FlighttaskRecoveryEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(flighttaskRecovery);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setMethod("flighttask_recovery");
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor inFlightWaylineStop(String inFlightWaylineId) {
        InFlightWaylineDeliver inFlightWaylineDeliver = new InFlightWaylineDeliver();
        inFlightWaylineDeliver.setInFlightWaylineId(inFlightWaylineId);
        BaseServicesEntity<InFlightWaylineDeliver> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(inFlightWaylineDeliver);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setMethod("in_flight_wayline_stop");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor inFlightWaylineRecover(String inFlightWaylineId) {
        InFlightWaylineRecoverEntity inFlightWaylineRecover = new InFlightWaylineRecoverEntity();
        inFlightWaylineRecover.setInFlightWaylineId(inFlightWaylineId);
        BaseServicesEntity<InFlightWaylineRecoverEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(inFlightWaylineRecover);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setMethod("in_flight_wayline_recover");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor inFlightWaylineCancel() {
        BaseServicesEntity baseServicesEntity = new BaseServicesEntity();
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setMethod("in_flight_wayline_cancel");
        baseServicesEntity.setData(null);
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor inFlightWaylineDeliver(String etag, String url, String inFlightWaylineId) {
        InFlightWaylineDeliver inFlightWaylineDeliver = new InFlightWaylineDeliver();
        InFlightWaylineDeliver.File file = new InFlightWaylineDeliver.File();
        inFlightWaylineDeliver.setInFlightWaylineId(inFlightWaylineId);
        file.setFingerprint(etag);
        file.setUrl(url);
        inFlightWaylineDeliver.setRthAltitude(400);
        inFlightWaylineDeliver.setRthMode(1);
        inFlightWaylineDeliver.setWaylinePrecisionType(0);
        inFlightWaylineDeliver.setExitWaylineWhenRcLost(0);
        inFlightWaylineDeliver.setOutOfControlAction(0);
        inFlightWaylineDeliver.setFile(file);
        BaseServicesEntity<InFlightWaylineDeliver> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setMethod("in_flight_wayline_deliver");
        baseServicesEntity.setData(inFlightWaylineDeliver);
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor flightId(String flightId) {
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.flightId = flightId;
        return flyServicesExecutor;
    }
    public FlyServicesExecutor bidAndTid(String bid, String tid) {
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.bid = bid;
        flyServicesExecutor.tid = tid;
        return flyServicesExecutor;
    }


    public FlyServicesExecutor returnHome() {
        BaseServicesEntity baseServicesEntity = new BaseServicesEntity();
        baseServicesEntity.setData(null);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("return_home");
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

//    @JSONField(name = "roll")
//    private Integer roll;
//    @JSONField(name = "pitch")
//    private Integer pitch;
//    @JSONField(name = "yaw")
//    private Integer yaw;
//    @JSONField(name = "throttle")
//    private Integer throttle;
//    @JSONField(name = "gimbal_pitch")
//    private Integer gimbalPitch;

    public FlyServicesExecutor stickControl(Integer roll,
                                            Integer pitch,
                                            Integer yaw,
                                            Integer throttle,
                                            Integer gimbalPitch) {
//        StickControlData stickControlData = new StickControlData();
//        //左右
//        stickControlData.setRoll(1024 + 660);
//        //俯仰
//        stickControlData.setPitch(1024 - 660);
//        //升降
//        stickControlData.setThrottle(1684);
//        //偏航
//        stickControlData.setYaw(1024);
//        //云台俯仰
//        stickControlData.setGimbalPitch(1024);

        StickControlData stickControlData = new StickControlData();
//        //左右
//        stickControlData.setRoll(1024 + 660);
//        //俯仰
//        stickControlData.setPitch(1024 - 660);
//        //升降
//        stickControlData.setThrottle(1684);
//        //偏航
//        stickControlData.setYaw(1024);
//        //云台俯仰
//        stickControlData.setGimbalPitch(1024);


        //左右
        stickControlData.setRoll(roll);
        //俯仰
        stickControlData.setPitch(pitch);
        //升降
        stickControlData.setThrottle(throttle);
        //偏航
        stickControlData.setYaw(yaw);
        //云台俯仰
        stickControlData.setGimbalPitch(gimbalPitch);


        stickControlData.setFreq(5);
        stickControlData.setDelayTime(100);

        BaseServicesEntity<StickControlData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setData(stickControlData);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("stick_control");

        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }


    public FlyServicesExecutor droneControl(Integer x,
                                            Integer y,
                                            Integer h,
                                            Integer w) {
//        StickControlData stickControlData = new StickControlData();
//        //左右
//        stickControlData.setRoll(1024 + 660);
//        //俯仰
//        stickControlData.setPitch(1024 - 660);
//        //升降
//        stickControlData.setThrottle(1684);
//        //偏航
//        stickControlData.setYaw(1024);
//        //云台俯仰
//        stickControlData.setGimbalPitch(1024);

        DroneControlData droneControlData = new DroneControlData();
//        //左右
//        stickControlData.setRoll(1024 + 660);
//        //俯仰
//        stickControlData.setPitch(1024 - 660);
//        //升降
//        stickControlData.setThrottle(1684);
//        //偏航
//        stickControlData.setYaw(1024);
//        //云台俯仰
//        stickControlData.setGimbalPitch(1024);


        //左右
        droneControlData.setH(h);
        droneControlData.setY(y);
        droneControlData.setX(x);
        droneControlData.setW(w);
        droneControlData.setSeq(1);

        BaseServicesEntity<DroneControlData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setData(droneControlData);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("drone_control");

        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println("drc_控制"+flyServicesExecutor);
        return flyServicesExecutor;
    }

    //更新目标点
    public FlyServicesExecutor flyToPointUpdate(FlyToPointUpdateRequestDTO flyToPointUpdateRequestDTO) {

        FlyToPointUpdateData flyToPointUpdateData = BeanUtil.copyProperties(flyToPointUpdateRequestDTO, FlyToPointUpdateData.class);
        //左右
        BaseServicesEntity<FlyToPointUpdateData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setData(flyToPointUpdateData);
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("fly_to_point_update");

        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println("更新目标点"+JSONObject.toJSONString(flyServicesExecutor));
        System.out.println("更新目标点Executor");
        return flyServicesExecutor;
    }


    public FlyServicesExecutor droneEmergencyStop() {
        BaseServicesEntity baseServicesEntity = new BaseServicesEntity();
//        baseServicesEntity.setBid(UUID.randomUUID().toString());
//        baseServicesEntity.setTid(UUID.randomUUID().toString());
//        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setData(new DroneEmergencyStopData());
        baseServicesEntity.setMethod("drone_emergency_stop");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor flighttaskUndo(List<String> list) {
//        List<String> list = new ArrayList<>();
//        list.add("032265fe-ad72-41f7-9a72-fab5afcfbd95");
        FlighttaskUndoData undoData = new FlighttaskUndoData();
        undoData.setFlightIds(list);

        BaseServicesEntity<FlighttaskUndoData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("flighttask_undo");
        baseServicesEntity.setData(undoData);
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor drcModeEnter(DrcModeEnterRequestDTO drcModeEnterRequestDTO) {
        DrcModeEnterData.MqttBroker mqttBroker = new DrcModeEnterData.MqttBroker();
        mqttBroker.setAddress("120.211.119.131:1883");
        mqttBroker.setClientId("drc-" + drcModeEnterRequestDTO.getGateway());
        mqttBroker.setUsername("feisuan");
        mqttBroker.setPassword("feisuan123456");
        mqttBroker.setEnableTls(drcModeEnterRequestDTO.getEnableTls());
        mqttBroker.setExpireTime(drcModeEnterRequestDTO.getExpireTime());
        DrcModeEnterData drcModeEnterData = new DrcModeEnterData();
        drcModeEnterData.setMqttBroker(mqttBroker);
        drcModeEnterData.setHsiFrequency(drcModeEnterRequestDTO.getHsiFrequency());
        drcModeEnterData.setOsdFrequency(drcModeEnterRequestDTO.getOsdFrequency());
        BaseServicesEntity<DrcModeEnterData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setData(drcModeEnterData);
        baseServicesEntity.setMethod("drc_mode_enter");

        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor flightTaskExecuteTask() {
        FlightTaskExecuteData flightTaskExecuteData = new FlightTaskExecuteData();
        flightTaskExecuteData.setFlightId(flightId);
//        FlightTaskExecuteData.MultiDockTask multiDockTask = new FlightTaskExecuteData.MultiDockTask();
//
//        //图传连接拓扑
//        FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo wirelessLinkTopo = new FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo();
//
//
//        //加密编码
//        wirelessLinkTopo.setSecretCode(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 1, 0, 0, 0, 105, 197, 199, 164, 36, 3, 132, 219, 205, 202, 138, 222, 51, 224, 108, 237, 213, 232, 159, 202)));
//
//        //飞行器对频信息
//        FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.CenterNode centerNode = new FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.CenterNode();
//        centerNode.setSn("1581F8HGX252500A0022");
//        centerNode.setSdrId(1874468847L);
//
//        //机场或遥控器对频信息
//        List<FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.LeafNode> leafNodeList = new ArrayList<>();
////        FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.LeafNode leafNode = new FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.LeafNode();
////        leafNode.setControlSourceIndex(1);
////        //设备sn
////        leafNode.setSn("1581F8HGX252500A0022");
////        //扰码信息
////        leafNode.setSdrId(1874468847L);
//
//        FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.LeafNode leafNode2 = new FlightTaskExecuteData.MultiDockTask.WirelessLinkTopo.LeafNode();
//        leafNode2.setControlSourceIndex(0);
//        leafNode2.setSn("8UUXN2800A00HX");
//        leafNode2.setSdrId(3216035234L);
//        //TODO
////        leafNode2.setValid(true);
//
//
////        leafNodeList.add(leafNode);
//        leafNodeList.add(leafNode2);
//
//
//        wirelessLinkTopo.setLeafNodes(leafNodeList);
//        wirelessLinkTopo.setCenterNode(centerNode);
//
//
//        multiDockTask.setWirelessLinkTopo(wirelessLinkTopo);
//
//        FlightTaskExecuteData.MultiDockTask.DockInfo dockInfo = new FlightTaskExecuteData.MultiDockTask.DockInfo();
//        dockInfo.setDockType("takeoff");
//        dockInfo.setIndex(777777);
//        dockInfo.setHomePositionIsValid(1);
//        dockInfo.setHeading(112.82346343994141);
//        dockInfo.setHeight(13.187554359436035);
//        dockInfo.setSn("8UUXN2800A00HX");
//        dockInfo.setLongitude(118.18468404285719);
//        dockInfo.setLatitude(39.596918502111379);
//
//        FlightTaskExecuteData.MultiDockTask.DockInfo.RtcmInfo rtcmInfo = new FlightTaskExecuteData.MultiDockTask.DockInfo.RtcmInfo();
//        rtcmInfo.setHost("120.4.119.140");
//        rtcmInfo.setPort("36568");
//        rtcmInfo.setMountPoint("RTCM33_GRCEJ");
//        //未标定rtk
//        rtcmInfo.setSourceType(0);
//        rtcmInfo.setRtcmDeviceType(1);
//        dockInfo.setRtcmInfo(rtcmInfo);
//
//
//        List<FlightTaskExecuteData.MultiDockTask.DockInfo> dockInfoList = new ArrayList<>();
//        dockInfoList.add(dockInfo);
////        multiDockTask.setDockInfos(dockInfoList);
//
//        //蛙跳
//        multiDockTask.setDockInfos(dockInfoList);
////        multiDockTask.setDockInfos(new ArrayList<>());
//        flightTaskExecuteData.setMultiDockTask(multiDockTask);

        // ===== 4. 构建基础请求 =====
        BaseServicesEntity<FlightTaskExecuteData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(flightTaskExecuteData);
//        baseServicesEntity.setBid(UUID.randomUUID().toString());
//        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setBid(bid);
        baseServicesEntity.setTid(tid);
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("flighttask_execute");

        // 处理请求
//        abstractLiveCameraChange.process(JSONObject.toJSONString(baseServicesEntity));

        FlyServicesExecutor flyTask = new FlyServicesExecutor();
        flyTask.setFlightId(flightId);
        flyTask.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyTask;
    }


    public FlyServicesExecutor takeoffToPoint(
            Float commanderFlightHeight,
            Integer commanderModeLostAction,
            Integer maxSpeed,
            Integer commanderFlightMode,
            Integer rthMode,
            Integer rcLostAction,
            Integer rthAltitude,
            Float targetHeight,
            Integer securityTakeoffHeight,
            BigDecimal targetLongitude,
            BigDecimal targetLatitude
    ) {

        TakeoffToPointData takeoffToPointData = new TakeoffToPointData();

        takeoffToPointData.setCommanderFlightHeight(commanderFlightHeight);
        takeoffToPointData.setCommanderModeLostAction(commanderModeLostAction);
        takeoffToPointData.setFlightId(flightId);
        takeoffToPointData.setMaxSpeed(maxSpeed);
        takeoffToPointData.setCommanderFlightMode(commanderFlightMode);
        takeoffToPointData.setRthMode(rthMode);
        takeoffToPointData.setRcLostAction(rcLostAction);
        takeoffToPointData.setRthAltitude(rthAltitude);
        takeoffToPointData.setTargetHeight(targetHeight);
        takeoffToPointData.setSecurityTakeoffHeight(securityTakeoffHeight);

        takeoffToPointData.setTargetLongitude(targetLongitude);
        takeoffToPointData.setTargetLatitude(targetLatitude);

//        takeoffToPointData.setCommanderFlightHeight(400.240487783050537f);
//        takeoffToPointData.setCommanderModeLostAction(1);
//        takeoffToPointData.setFlightId(flightId);
//        takeoffToPointData.setMaxSpeed(20);
//        takeoffToPointData.setCommanderFlightMode(1);
//        takeoffToPointData.setRthMode(1);
//        takeoffToPointData.setRcLostAction(2);
//        takeoffToPointData.setRthAltitude(100);
//        takeoffToPointData.setTargetHeight(400.240387783050537f);
//        takeoffToPointData.setSecurityTakeoffHeight(400);
//
//        takeoffToPointData.setTargetLongitude(new BigDecimal("118.18467623581363"));
//        takeoffToPointData.setTargetLatitude(new BigDecimal("39.5969342494349"));


//        takeoffToPointData.setFlightSafetyAdvanceCheck(1);
        BaseServicesEntity<TakeoffToPointData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setData(takeoffToPointData);
        baseServicesEntity.setMethod("takeoff_to_point");

        FlyServicesExecutor flyTask = new FlyServicesExecutor();
        flyTask.setFlightId(flightId);
        flyTask.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println(takeoffToPointData);
        return flyTask;
    }


    public FlyServicesExecutor flyToPoint(BigDecimal maxSpeed, BigDecimal height, BigDecimal latitude, BigDecimal longitude) {

        String json = "{" +
                "\"bid\": \"" + UUID.randomUUID().toString() + "\"," +
                "\"data\": {" +
                "\"fly_to_id\": \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx\"," +
                "\"max_speed\": " + maxSpeed + "," +
                "\"points\": [" +
                "{" +
                "\"height\": " + height + "," +
                "\"latitude\": " + latitude + "," +
                "\"longitude\": " + longitude + "" +
                "}" +
                "]" +
                "}," +
                "\"tid\": \"" + UUID.randomUUID().toString() + "\"," +
                "\"timestamp\": " + System.currentTimeMillis() + "," +
                "\"method\": \"fly_to_point\"" +
                "}";

        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(json);
        flyServicesExecutor.setFlightId(flightId);
        return flyServicesExecutor;
    }


    public FlyServicesExecutor flightTaskPrepare(String url, String md5, String gateway) throws Exception {
//        flightId = UUID.randomUUID().toString().replace("-", "");


        Long time = System.currentTimeMillis();
        FlightTaskPrepareData flightTaskPrepareData = new FlightTaskPrepareData();
        flightTaskPrepareData.setRthMode(1);
        FlightTaskPrepareData.BreakPoint breakPoint = new FlightTaskPrepareData.BreakPoint();
        //断点序号
        breakPoint.setIndex(0);
        ////当前航段进度
        breakPoint.setProgress(new BigDecimal("0.00"));
        //{"0":"在航段上","1":"在航点上"}
        breakPoint.setState(0);
        //航线id
        breakPoint.setWaylineId(0);
        //"0":"智能高度","1":"设定高度"
        breakPoint.setRthMode(1);
        flightTaskPrepareData.setBreakPoint(breakPoint);
        FlightTaskPrepareData.ExecutableConditions executableConditions = new FlightTaskPrepareData.ExecutableConditions();
        //存储容量
        executableConditions.setStorageCapacity(20);
        flightTaskPrepareData.setExecutableConditions(executableConditions);
        //执行时间
        flightTaskPrepareData.setExecuteTime(time);
        //航线失控动作 "0":"继续执行航线任务","1":"退出航线任务，执行遥控器失控动作"
        flightTaskPrepareData.setExitWaylineWhenRcLost(0);


        FlightTaskPrepareData.File flightTaskPrepareDataFile = new FlightTaskPrepareData.File();
//        File file = FileUtils.ossUrlToFile(url);
//        String s = FileUtils.calculateMD5(file);
//        FileUtils.cleanTempFile(file);
        //设置文件的md5
        flightTaskPrepareDataFile.setFingerPrint(md5);
        //设置文件的url
        flightTaskPrepareDataFile.setUrl(url);
        flightTaskPrepareData.setFile(flightTaskPrepareDataFile);

//        flightTaskPrepareData.setFlightId(UUID.randomUUID().toString().replace("-", ""));
        flightTaskPrepareData.setFlightId(flightId);
        //"0":"关闭","1":"开启" 飞行安全预检查
        flightTaskPrepareData.setFlightSafetyAdvanceCheck(0);
//        flightTaskPrepareData.setFlightSafetyAdvanceCheck(false);
        //遥控器失控动作 "0":"返航","1":"悬停","2":"降落"
        flightTaskPrepareData.setOutOfControlAction(0);

        //任务就绪条件
        FlightTaskPrepareData.ReadyConditions readyConditions = new FlightTaskPrepareData.ReadyConditions();
        //电池容量
        readyConditions.setBatteryCapacity(60);
        //任务可执行时段的开始时间
        readyConditions.setBeginTime(time + 16000);
        //任务可执行时段的结束时间
        readyConditions.setEndTime(System.currentTimeMillis() + 120000);
        flightTaskPrepareData.setReadyConditions(readyConditions);
        //返航高度
        flightTaskPrepareData.setRthAltitude(60);

        //是否开启模拟器任务 0不开启
        FlightTaskPrepareData.SimulateMission simulateMission = new FlightTaskPrepareData.SimulateMission();
        simulateMission.setIsEnable(0);
        simulateMission.setLatitude(new BigDecimal("30.249386"));
        simulateMission.setLongitude(new BigDecimal("119.902069"));
        simulateMission.setAltitude(new BigDecimal("120.000000"));
        flightTaskPrepareData.setSimulateMission(simulateMission);
//        flightTaskPrepareData.setFlightSafetyAdvanceCheck(1);
        //任务类型 "0":"立即任务","1":"定时任务","2":"条件任务"
        flightTaskPrepareData.setTaskType(0);
        //航线精度类型 "0":"GPS 任务","1":"高精度 RTK 任务"
        flightTaskPrepareData.setWaylinePrecisionType(0);


        BaseServicesEntity<FlightTaskPrepareData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(flightTaskPrepareData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("flighttask_prepare");
        String data = JSONObject.toJSONString(baseServicesEntity);
        FlyServicesExecutor flyTask = new FlyServicesExecutor();
        flyTask.setData(data);
        flyTask.setFlightId(flightId);
//        abstractServicesHandler.process(JSONObject.toJSONString(baseServicesEntity));
        return flyTask;
    }


    public FlyServicesExecutor execute() throws MqttException {
        System.out.println(data);
        System.out.println(gateway);
        abstractServicesHandler.process(data, gateway);
        FlyServicesExecutor flyTask = this;
        flyTask.setFlightId(flightId);
        flyTask.setData(data);

        return flyTask;
    }

    public FlyServicesExecutor drcExecute() throws MqttException {
        log.info(data);
        System.out.println(gateway);
        abstractServicesHandler.processDrc(data, gateway);
        FlyServicesExecutor flyTask = new FlyServicesExecutor();
        flyTask.setFlightId(flightId);
        flyTask.setData(data);
        System.out.println("飞行控制"+flyTask);
        return flyTask;
    }


//    public FlyServicesExecutor liveStartPush() {
//        LiveStartPushData liveStartPushData = new LiveStartPushData();
//        liveStartPushData.setUrlType(1);
//        liveStartPushData.setUrl("rtmp://120.211.119.131:1935/live/test");
//        liveStartPushData.setVideoId("1581F8HGX252500A0022/99-0-0/normal-0");
//        liveStartPushData.setVideoQuality(4);
//        BaseServicesEntity<LiveStartPushData> baseServicesEntity = new BaseServicesEntity<>();
//        baseServicesEntity.setData(liveStartPushData);
//        baseServicesEntity.setBid(UUID.randomUUID().toString());
//        baseServicesEntity.setTid(UUID.randomUUID().toString());
//        baseServicesEntity.setTimestamp(System.currentTimeMillis());
//        baseServicesEntity.setMethod("live_start_push");
//        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
//        flyServicesExecutor.setFlightId(flightId);
//        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
//        return flyServicesExecutor;
//    }

    public FlyServicesExecutor flightAuthorityGrab() throws MqttException {

        BaseServicesEntity<FlightAuthorityGrabData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(new FlightAuthorityGrabData());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("flight_authority_grab");
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setFlightId(flightId);
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor flighttaskPause() {
        BaseServicesEntity<FlighttaskPauseEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(new FlighttaskPauseEntity());
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("flighttask_pause");
        FlyServicesExecutor flyServicesExecutor = new FlyServicesExecutor();
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }

    public FlyServicesExecutor videoStorageSet(List<String> videoStorageSettings, String payloadIndex) {
        VideoStorageSetEntity videoStorageSetEntity = new VideoStorageSetEntity();
        videoStorageSetEntity.setVideoStorageSettings(videoStorageSettings);
        videoStorageSetEntity.setPayloadIndex(payloadIndex);
        BaseServicesEntity<VideoStorageSetEntity> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(videoStorageSetEntity);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("video_storage_set");
        FlyServicesExecutor flyServicesExecutor = this;
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }
}
